/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata.api;

import android.util.Log;

import app.transittool.model.Agency;
import app.transittool.model.DataRetrievalException;
import app.transittool.model.Stop;

import trs.wmata.TrainStationEntrance;
import static trs.wmata.api.ApiXml.DESCRIPTION;
import static trs.wmata.api.ApiXml.LAT;
import static trs.wmata.api.ApiXml.LON;
import static trs.wmata.api.ApiXml.NAME;
import static trs.wmata.api.ApiXml.STATION_CODE1;
import static trs.wmata.api.ApiXml.STATION_CODE2;
import static trs.wmata.api.ApiXml.STATION_ENTRANCE;

import java.util.ArrayList;

import org.xml.sax.Attributes;

public final class TrainStationEntrances extends ApiResponse {
    public static final String TAG = "TrainStationEntrances";
    private final Agency agency;
    private final ArrayList<TrainStationEntrance> stationEntrances = new ArrayList<TrainStationEntrance>(5);
    private Stop station;
    private String name;

    // private String description;
    private double lat;
    private double lon;

    public TrainStationEntrances(final Agency agency) {
        this.agency = agency;
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        // if (StationEntrancesResp.equals(lName) ) {
        // } else if (Entrances.equals(lName) ) {
        if (STATION_ENTRANCE.equals(lName)) {
            station = null;
            name = null;
            // description = null;
            lat = -1;
            lon = -1;
        } else if (DESCRIPTION.equals(lName)) {
            doCaptureChars = true;

            // } else if (ID.equals(lName) ) {
        } else if (LAT.equals(lName)) {
            doCaptureChars = true;
        } else if (LON.equals(lName)) {
            doCaptureChars = true;
        } else if (NAME.equals(lName)) {
            doCaptureChars = true;
        } else if (STATION_CODE1.equals(lName)) {
            doCaptureChars = true;
        } else if (STATION_CODE2.equals(lName)) {
            doCaptureChars = true;
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        final String str = buff.toString().trim();

        // if (StationEntrancesResp.equals(lName) ) {
        // } else if (Entrances.equals(lName) ) {
        // } else
        if (STATION_ENTRANCE.equals(lName)) {
            if (isNotBlank(name) && (station != null)) {
                final TrainStationEntrance stationEntrance = new TrainStationEntrance(station, name, lat, lon);
                stationEntrances.add(stationEntrance);
            }

            // } else if (Description.equals(lName) ) {
            // description = str;
            // } else if (ID.equals(lName) ) {
        } else if (LAT.equals(lName)) {
            try {
                lat = Double.valueOf(str);
            } catch (final NumberFormatException e) {
                lat = 0;
            }
        } else if (LON.equals(lName)) {
            try {
                lon = Double.valueOf(str);
            } catch (final NumberFormatException e) {
                lon = 0;
            }
        } else if (NAME.equals(lName)) {
            name = str;
        } else if (STATION_CODE1.equals(lName)) {
            station = getStopByCode(str);
        } else if (STATION_CODE2.equals(lName)) {
            if (station == null) {
                station = getStopByCode(str);
            }
        }
    }

    public ArrayList<TrainStationEntrance> getStationEntrances() {
        return stationEntrances;
    }

    private Stop getStopByCode(final String str) {
        if (isNotBlank(str)) {
            try {
                return agency.getStopByCode(str);
            } catch (final DataRetrievalException e) {
                Log.w(TAG, "Problems obtaining data for stop:" + str, e);
            }
        }

        return null;
    }

    private boolean isNotBlank(final String str) {
        return (str != null) && (str.trim().length() > 0);
    }

    private boolean doCaptureChars = false;

    @Override
    public boolean doCaptureChars() {
        return doCaptureChars;
    }
}
